package com.badibadi.activity;

import com.badibadi.adapter.PinLunGYAdapter;
import com.badibadi.fragment.LogReadingDanjiFragment0;
import com.badibadi.fragment.LogReadingDanjiFragment0.getEditText;
import com.badibadi.fragment.LogReadingDanjiFragment0.setActivityView;
import com.badibadi.fragment.LogReadingDanjiFragment1;
import com.badibadi.fragment.LogReadingDanjiFragment2;
import com.badibadi.fragment.LogReadingFragment1;
import com.badibadi.fragment.LogReadingFragment2;
import com.badibadi.infos.LogReadingDanji_Model;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.TabHostUtils;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.SinaExpressionActivity;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;
import com.libjph.util.StringUtil;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;

//后来改了；只需要传入aid（单记）就行了 bianji 
public class LogReadingDanjiActivity extends BaseActivity {
	private TabHost mTabHost;
	private FragmentManager manager;
	private String aid;// 单记的id
	private String rid;// 篇的id
	private String f_uid;// 发起者的id
	private String admin_id;// 俱乐部管理员管理员id 如果是个人的为null
	private String cid;// 俱乐部id 如果是个人的为null
	private int position;// 用于判断第几记
	private LogReadingDanjiFragment0 fragment;
	private LogReadingDanjiFragment1 fragment1;
	private LogReadingFragment2 fragment2;
	private String KEY0 = "TITLE";
	private String Title;
	LogReadingDanji_Model logReadingDanji_Model;
	// title的isRun
	private boolean isRun = true;
	TextView tv_title;
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:// 标题
				try {
					Utils.ExitPrgress(LogReadingDanjiActivity.this);
					tv_title.setText("" + msg.getData().getString(KEY0));
					rid = logReadingDanji_Model.getRecord_id();// 篇的id
					isRun = false;
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.activity_log_reading_danji);

		aid = getIntent().getStringExtra("aid");
		// position=getIntent().getIntExtra("position", 1);
		rid = getIntent().getStringExtra("rid");// rid是篇的id
//		   

		// f_uid=getIntent().getStringExtra("f_uid");
		// cid=getIntent().getStringExtra("cid");
		// admin_id=getIntent().getStringExtra("admin_id");

		// if(aid==null)
		// {
		// aid="1116";
		// }
		tv_title = (TextView) this.findViewById(R.id.title);

		this.findViewById(R.id.return_btn).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						LogReadingDanjiActivity.this.finish();
					}
				});

		manager = getSupportFragmentManager();
		settingTabHost();
		LoadingFragment(0);

	}

	private void gengxinTitle() {
		// 1.更新Title的线程
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				while (isRun) {
					//    
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						   
					}

					fragment.setRecordTitle(new setActivityView() {
						//
						@Override
						public void setRecordData(
								final LogReadingDanji_Model data) {
							// TODO Auto-generated method stub
							if (data != null) {
								logReadingDanji_Model = data;
								Message msg = new Message();
								msg.what = 1;
								Bundle bun = new Bundle();
								bun.putString(KEY0, "" + data.getTitle());
								msg.setData(bun);
								mHandler.sendMessage(msg);

							}
						}
					});

				}

			}
		}).start();

	}

	private void settingTabHost() {
		// TODO Auto-generated method stub
		mTabHost = (TabHost) this.findViewById(R.id.log_reading_tabHost);
		mTabHost.setup();
		mTabHost.addTab(mTabHost
				.newTabSpec("logRead one")
				.setIndicator(getResources().getString(R.string.record),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab1));
		mTabHost.addTab(mTabHost
				.newTabSpec("logRead two")
				.setIndicator(getResources().getString(R.string.picture),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab2));
		mTabHost.addTab(mTabHost
				.newTabSpec("logRead three")
				.setIndicator(getResources().getString(R.string.Icon),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab3));
		TabHostUtils.SetTabHost(this, mTabHost);
		mTabHost.setOnTabChangedListener(new OnTabChangeListener() {

			@Override
			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub

				switch (mTabHost.getCurrentTab()) {
				case 0:
					LoadingFragment(0);
					break;
				case 1:
					LoadingFragment(1);
					break;
				case 2:
					LoadingFragment(2);
					break;
				}
			}
		});

	}

	protected void LoadingFragment(int num) {
		// TODO Auto-generated method stub
		gengxinTitle();
		FragmentTransaction transaction = manager.beginTransaction();
		switch (num) {
		case 0:
			FragmentTransaction transaction2 = manager.beginTransaction();
			fragment = LogReadingDanjiFragment0.getInstance();
			Bundle bun = new Bundle();
			bun.putString("aid", aid);
			bun.putInt("position", position);
			bun.putString("rid", rid);
			bun.putString("f_uid", f_uid);
			bun.putString("cid", cid);
			bun.putString("admin_id", admin_id);

			fragment.setArguments(bun);
			transaction2.replace(R.id.log_rading_layout, fragment,
					"logReadingFragment0");
			transaction2.commit();
			break;
		case 1:
//			   
			FragmentTransaction transaction3 = manager.beginTransaction();
			fragment1 = LogReadingDanjiFragment1.getInstance();
			Bundle bun1 = new Bundle();
			bun1.putString("rid", rid);
			fragment1.setArguments(bun1);
			transaction3.replace(R.id.log_rading_layout, fragment1,
					"logReadingFragment1");
			transaction3.commit();
			break;
		case 2:
			FragmentTransaction transaction4 = manager.beginTransaction();
			fragment2 = LogReadingFragment2.getInstance();
			Bundle bun2 = new Bundle();
			bun2.putString("rid", rid);
			fragment2.setArguments(bun2);
			transaction4.replace(R.id.log_rading_layout, fragment2,
					"logReadingFragment2");
			transaction4.commit();
			break;
		}
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, final Intent arg2) {
		// TODO Auto-generated method stub

		if (arg0 == PinLunGYAdapter.BIAO_QING
				&& arg1 == SinaExpressionActivity.ACTIVITY_BIAOQING) {
			if (arg2 != null) {
				fragment.getSend_Content(new getEditText() {

					@Override
					public void getEditText(EditText send_content) {
						// TODO Auto-generated method stub
						if (send_content != null) {
							// String
							// content=send_content.getText().toString().trim();
							// String content2=arg2.getStringExtra("result");
							// send_content.setText(content+content2);
							String content2 = arg2.getStringExtra("result");
							int index = send_content.getSelectionStart();
							Editable edit = send_content.getEditableText();
							edit.insert(index, content2);
						}
					}
				});
			}
		}

		super.onActivityResult(arg0, arg1, arg2);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		if (Constants.is_shuaxin) {
			Utils.ExitPrgress(LogReadingDanjiActivity.this);
			Constants.is_shuaxin = false;
			LoadingFragment(0);
		}
	}
}
